/*
 * Menu.cpp
 *
 *  Created on: 2011-03-30
 *      Author: tmacyszyn
 */

#include "Menu/Menu.h"
#include <HitTheLines.h>

Menu::Menu(Font* font, void* parent)
{
	this->font = font;
	this->parent = parent;
}

Menu::~Menu()
{
}

void
Menu::Init()
{
}

void
Menu::AddItem(String title, void (*OnClickHandler)(void*))
{
	int x = MenuItem::width * 0.5f * HitTheLines::getInstance()->globalWidth;
	int y = MenuItem::height * 0.5f * HitTheLines::getInstance()->globalHeight;

	HitTheLines::updateTextureSize(x, y);

	//Construct new font's canvas
	textCanvas = new Canvas();//GetAppFrame()->GetCanvasN();
	Rectangle canvasBounds = Rectangle(0,0, x,y);//GetAppFrame()->GetFrame()->GetBounds();
	textCanvas->Construct(canvasBounds);

	textCanvas->SetBackgroundColor(Color(0, 0, 0, 255));
	textCanvas->Clear();

	enrichedText = new EnrichedText();
	enrichedText->Construct(Dimension(x, y));

	enrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);
	enrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
	enrichedText->SetTextWrapStyle(TEXT_WRAP_NONE);
	enrichedText->SetTextAbbreviationEnabled(false);

    TextElement* pTextElement = new TextElement();
    pTextElement->Construct(title);
    enrichedText->Add(*pTextElement);

    pTextElement->SetTextColor(Color(255,255,255,255));
    pTextElement->SetFont(*font);

	textCanvas->DrawText(Point(0, 0), *enrichedText);

	enrichedText->RemoveAllTextElements(true);

	Osp::Graphics::Opengl::GLuint textsTexture;

	BufferInfo bufferInfo;
	textCanvas->Lock(bufferInfo);
	{
		glGenTextures(1, &textsTexture);
		glBindTexture(GL_TEXTURE_2D, textsTexture);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, canvasBounds.width, canvasBounds.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bufferInfo.pPixels);
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	}
	textCanvas->Unlock();

	delete textCanvas;
	delete enrichedText;

	items.push_back(new MenuItem(title, OnClickHandler, textsTexture));
}

bool
Menu::HandleInput(float x, float y)
{
	// iterating items to check if it was clicked
	vector<MenuItem*>::iterator iter;

	for(iter = items.begin(); iter != items.end(); iter++)
	{
		MenuItem* item = *iter;
		float xi = item->posX + posX;
		float yi = item->posY + posY;
		if (x > xi && x < xi + item->width && y > yi - item->height && y < yi)
		{
			if (item->OnClickHandler != NULL)
			{
				item->OnClickHandler(parent);
			}
			return true;
		}
	}

	return false;
}

bool
Menu::IsPointInside(float x, float y)
{
	return (x > posX && x < posX + width && y > posY - height && y < posY);
}

void
Menu::Build()
{
	if (items.size() < 1)
	{
		AppLog("[Menu] Items list is empty\n");
		return;
	}

	float totalHeight = MenuItem::height * items.size() + MenuItem::gap * (items.size() - 1);
	posY = totalHeight / 2;
	posX = -MenuItem::width / 2;
	width = MenuItem::width;
	height = totalHeight;

	vector<MenuItem*>::iterator iter;

	float offsetY = 0;
	for(iter = items.begin(); iter != items.end(); iter++)
	{
		MenuItem* item = *iter;
		item->posY = offsetY;
		offsetY -= MenuItem::height + MenuItem::gap;
	}
}

void
Menu::Render()
{
	vector<MenuItem*>::iterator iter;
	for(iter = items.begin(); iter != items.end(); iter++)
	{
		MenuItem* item = *iter;
		item->Render(posX, posY);
	}
}

void
Menu::CleanUp()
{
	vector<MenuItem*>::iterator iter;
	for(iter = items.begin(); iter != items.end(); iter++)
	{
		MenuItem* item = *iter;
		item->CleanUp();
		delete item;
	}
}
